class Solution:
    def sortString(self, s: str) -> str:
        mapper={}
        for i in s:
            try:
                mapper[i]+=1
            except KeyError:
                mapper[i]=1
        vocab=list(mapper.keys())
        vocab.sort()
        result=""
        count=0
        direct=1
        head=0
        tail=len(vocab)-1
        while count<len(s):
            if direct>0:
                while head<len(vocab) and mapper[vocab[head]]<=0:
                    head+=1
                if head==len(vocab):
                    direct=-1
                    head=0
                    continue
                result+=vocab[head]
                mapper[vocab[head]]-=1
                head+=1
            else:
                while tail>-1 and mapper[vocab[tail]]<=0:
                    tail-=1
                if tail==-1:
                    direct=1
                    tail=len(vocab)-1
                    continue
                result+=vocab[tail]
                mapper[vocab[tail]]-=1
                tail-=1
            count+=1
        return result

'''
执行用时：
80 ms
, 在所有 Python3 提交中击败了
71.30%
的用户
内存消耗：
13.5 MB
, 在所有 Python3 提交中击败了
34.25%
的用户
'''